class Solution {
public:
    int row, col;
    int dx[4] = { 0, 0, 1, -1 };
    int dy[4] = { 1, -1, 0, 0 };
    bool sign[101][101];
    int nearestExit(vector<vector<char>>& maze, vector<int>& entrance) {
        int ret = 0;
        row = maze.size();
        col = maze[0].size();
        queue<pair<int, int>> q;
        q.push({ entrance[0], entrance[1] });
        sign[entrance[0]][entrance[1]] = true;
        while (!q.empty())
        {
            int size = q.size();
            ret++;
            while (size--)
            {
                auto [i, j] = q.front();
                q.pop();
                for (int k = 0; k < 4; k++)
                {
                    int x = i + dx[k];
                    int y = j + dy[k];
                    if (x >= 0 && x < row && y >= 0 && y < col && maze[x][y] == '.' && !sign[x][y])
                    {
                        if (x == 0 || y == 0 || x == row - 1 || y == col - 1) return ret;
                        q.push({ x, y });
                        sign[x][y] = true;
                    }
                }
            }
        }
        return -1;
    }
};
class Solution {
public:
    char modify[4] = { 'A', 'C', 'G', 'T' };

    int minMutation(string startGene, string endGene, vector<string>& bank) {
        unordered_set<string> hash(bank.begin(), bank.end());
        unordered_map<string, bool> sign;
        if (!hash.count(endGene)) return -1;
        if (startGene == endGene) return 0;
        int ret = 0;
        queue<string> q;
        q.push(startGene);
        sign[startGene] = true;
        while (!q.empty())
        {
            ret++;
            int size = q.size();
            while (size--)
            {
                string top = q.front();
                q.pop();
                for (int i = 0; i < 8; i++)
                {
                    string num = top;
                    for (int k = 0; k < 4; k++)
                    {
                        num[i] = modify[k];
                        if (hash.count(num) && !sign[num])
                        {
                            if (num == endGene) return ret;
                            q.push(num);
                            sign[num] = true;
                        }
                    }
                }
            }
        }
        return -1;
    }
};